package com.liaowei.user.controller;

import com.liaowei.common.BaseResponse;
import com.liaowei.common.ResultUtils;
import com.liaowei.model.domain.Article;
import com.liaowei.model.param.ArticleHistoryParam;
import com.liaowei.user.model.param.PageArticleHistoryParam;
import com.liaowei.user.service.UserArticleHistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import java.util.List;

@RestController
@RequestMapping("articles/histories")
@RequiredArgsConstructor
public class UserArticleHistoryController {

    private final UserArticleHistoryService userArticleHistoryService;


    // 分页查询自己的文章历史记录
    @PostMapping("page")
    public BaseResponse<List<Article>> articleHistory(@RequestBody
                                                      @Validated
                                                      PageArticleHistoryParam historyParam) {
        return ResultUtils.success(userArticleHistoryService.searchArticleHistoryPage(historyParam));
    }

    // 根据用户id和文章id删除文章记录
    @DeleteMapping("{userId}/{articleId}")
    public BaseResponse<String> deleteArticleHistoryByUserIdAndArticleId(@PathVariable
                                                                         @Min(value = 1, message = "请输入合法id")
                                                                         Long userId,
                                                                         @PathVariable
                                                                         @Min(value = 1, message = "请输入合法id")
                                                                         Long articleId) {
        return ResultUtils.success(userArticleHistoryService.
                removeArticleHistoryByUserIdAndArticleId(userId, articleId));
    }


    // 新增文章历史记录
    @PostMapping
    public BaseResponse<String> addArticleHistory(@RequestBody @Validated ArticleHistoryParam postHistoryParam) {
        return ResultUtils.success(userArticleHistoryService.addArticleHistory(postHistoryParam));
    }
}
